﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastUtil.Base;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Element")]
    public class ElementController : FastTemplateController<Frame_Element>
    {
        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Element/Element_List.cshtml");
        }
        [Route("Add")]
        public ActionResult Add()
        {
            return View("~/Views/Frame/Element/Element_Add.cshtml");
        }
        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Element/Element_Edit.cshtml");
        }

        [HttpPost, Route("FindList")]
        public override ActionResult FindList()
        {
            JObject Params = GetParams();
            string ElementTitle = Convert.ToString(Params["ElementTitle"]);
            if (!string.IsNullOrEmpty(ElementTitle))
            {
                ListWhereText += string.Format(" and ElementTitle like '%{0}%'", ElementTitle);
            }
            return base.FindList();
        }

        [HttpPost, Route("FindAllEnableList")]
        public ActionResult FindAllEnableList()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            List<Frame_Element> list = DB_Fast.FindAll(" and ElementStatus=1");
            JArray array = new JArray();
            foreach (Frame_Element Element in list)
            {
                JObject obj = new JObject();
                obj.Add("ElementTitle", Element.ElementTitle);
                string FilePath = System.Web.HttpContext.Current.Server.MapPath("~/" + Element.ElementTemplatePath);
                string TemplatContent = FileUtil.ReadFileContent(FilePath);
                obj.Add("TemplatContent", TemplatContent);
                obj.Add("ElementMoreUrl", Element.ElementMoreUrl);
                obj.Add("ElementDataUrl", CommonUtil.GetRootPath() + Element.ElementDataUrl);
                array.Add(obj);
            }
            result.Add("ElementList", array);
            return result;
        }

        [HttpPost, Route("FindDetail")]
        public override ActionResult FindDetail()
        {
            return base.FindDetail();
        }

        [HttpPost, Route("OperateAdd")]
        public override ActionResult OperateAdd(Frame_Element Entity)
        {
            return base.OperateAdd(Entity);
        }

        [HttpPost, Route("OperateUpdate")]
        public override ActionResult OperateUpdate()
        {
            return base.OperateUpdate();
        }

        [HttpPost, Route("OperateBatchDelete")]
        public override ActionResult OperateBatchDelete()
        {
            return base.OperateBatchDelete();
        }
    }
}
